﻿using System.Web.Mvc;
using PhotoBlog.Models.ClientModels;
using PhotoBlog.Models.Photos;
using PhotoBlog.Models.Users;

namespace PhotoBlog.Controllers
{
    public class PhotosController : Controller
    {
        private readonly IPhotoService photoService;
        private readonly IUserRepository userRepository;
        public PhotosController(IPhotoService photoService, IUserRepository userRepository)
        {
            this.photoService = photoService;
            this.userRepository = userRepository;
        }

        [Authorize]
        public ActionResult Upload()
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            return View();
        }

        [Authorize]
        public ActionResult UploadFiles(int id)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            var fileUploadVm = new FileUploadVm { AlbumId = id };
            return View(fileUploadVm);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Upload(FileUploadVm fileUploadVm)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            if (fileUploadVm != null)
            {
                fileUploadVm.Login = User.Identity.Name;
                photoService.SaveImages(fileUploadVm);
            }
            return RedirectToAction("Photos", "Home");
        }

        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id, int albumId)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            var login = User.Identity.Name;
            photoService.DeleteImage(login, id);
            return RedirectToAction("Edit", "Album", new { id = albumId });
        }

        [Authorize(Users = "admin")]
        public ActionResult UploadFile(int? entityId, int id)
        {
            var model = new FileUploadVm
            {
                AlbumId = id,
                UploadedRequestFiles = Request.Files,
                Login = User.Identity.Name
            };

            var statuses = photoService.SaveImageFiles(model);
            statuses.ForEach(x => x.url = Url.Action("DownloadFile", new { fileUrl = x.url, mimetype = x.type }));

            var viewresult = Json(new { files = statuses });
            //for IE8 which does not accept application/json
            if (Request.Headers["Accept"] != null && !Request.Headers["Accept"].Contains("application/json"))
                viewresult.ContentType = "text/plain";

            return viewresult;
        }

        [Authorize]
        public ActionResult DownloadFile(string fileUrl, string mimetype)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            if (System.IO.File.Exists(fileUrl))
                return File(fileUrl, mimetype);
            else
            {
                return new HttpNotFoundResult("File not found");
            }
        }
    }
}